package com.example.service;

import com.example.enmus.BookStatus;
import com.example.mapper.BookInfoMapper;
import com.example.model.BookInfo;
import com.example.model.PageRequest;
import com.example.model.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: bjc
 * Date: 2025-07-21
 * Time: 20:58
 */
@Service
public class BookService {

    @Autowired
    private BookInfoMapper bookInfoMapper;

    /**
     * 添加图书
     * @param bookInfo
     * @return
     */
    public Integer insertBook(BookInfo bookInfo){
        return bookInfoMapper.insertBook(bookInfo);
    }


    /**
     * 分页查询图书列表
     * @param pageRequest
     * @return
     */
    public PageResult<BookInfo> getBookListByPage(PageRequest pageRequest) {
        //1.查询总记录数
        Integer totalCount = bookInfoMapper.count();
        //2.查询当前页数据
        List<BookInfo> bookInfoList = bookInfoMapper.selectBookPage(pageRequest);

        //3.处理图书逻辑
        for (BookInfo bookInfo : bookInfoList) {
           bookInfo.setStatusCN(BookStatus.getDescByCode(bookInfo.getStatus()).getDesc());
        }
        return new PageResult<>(bookInfoList,totalCount,pageRequest);
    }



    /**
     * 根据id查询图书
     * @param id
     * @return
     */
    public BookInfo queryBookById(Integer id){
        //处理图书状态逻辑
        BookInfo bookInfo = bookInfoMapper.queryBookById(id);
        bookInfo.setStatusCN(BookStatus.getDescByCode(bookInfo.getStatus()).getDesc());
        return bookInfo;
    }

    /**
     * 修改图书信息
     * @param bookInfo
     * @return
     */
    public Integer updateBook(BookInfo bookInfo){
        return bookInfoMapper.updateBook(bookInfo);
    }

    /**
     * 删除图书
     * @param id
     * @return
     */
    public Integer deleteBook(Integer id){
        return bookInfoMapper.deleteBook(id);
    }

    /**
     * 批量删除图书
     * @param bookIds
     * @return
     */
    public Integer batchDeleteBook(List<Integer> bookIds){
        return bookInfoMapper.batchDeleteBook(bookIds);
    }
}
